package com.pda.yuanjing.pages.sycx;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.pda.yuanjing.R;
import com.pda.yuanjing.beanout.CodeQueryBeanOut;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ItemAdapter extends BaseAdapter {
    private Context mContext;
    LayoutInflater inflater;
    List<CodeQueryBeanOut.DataBean.ListInfoBean> mlists = new ArrayList<>();
    private int selectedPosition = -1;

    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }

    public ItemAdapter(Context context, List<CodeQueryBeanOut.DataBean.ListInfoBean> lists) {
        this.mContext = context;
        inflater = LayoutInflater.from(context);
        mlists = lists;
    }

    @Override
    public int getCount() {
        return mlists == null ? 0 : mlists.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_coded, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.ll.removeAllViews();
        for (int i = 0; i < mlists.get(position).getListResult().size(); i++) {
            LinearLayout layout = new LinearLayout(mContext);
            layout.setOrientation(LinearLayout.HORIZONTAL);
            LinearLayout.LayoutParams LP_MM = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            LP_MM.setMargins(6,10,6,0);
            layout.setLayoutParams(LP_MM);
            LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            TextView tv = new TextView(mContext);
            tv.setText(mlists.get(position).getListResult().get(i).getKey());
            tv.setTextSize(14);
            tv.setTextColor(mContext.getColor(R.color.black));
            tv.setLayoutParams(LP_WW);
            layout.addView(tv);
            TextView tv1 = new TextView(mContext);
            tv1.setText(mlists.get(position).getListResult().get(i).getValue());
            tv1.setTextSize(14);
            tv1.setLayoutParams(LP_WW);
            layout.addView(tv1);
            viewHolder.ll.addView(layout);
        }
        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.ll)
        LinearLayout ll;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

